/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */

package devbureau.fstore.util;

import devbureau.fstore.common.CustomerData;
import devbureau.fstore.common.CustomerDataImpl;
import devbureau.fstore.common.ItemDataImpl;
import devbureau.fstore.common.OrderData;
import devbureau.fstore.common.OrderDataImpl;
import devbureau.fstore.common.OrderLineData;
import devbureau.fstore.common.OrderLineDataImpl;
import devbureau.fstore.common.OrderStatus;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;


/**
 * Class keeps routines for creating objects from resultStes
 * @author SSheipak
 */

public class StoreHelper {

    public static final List<OrderData> parseOrders(ResultSet rs) throws SQLException{
        List<OrderData> orders = new ArrayList<OrderData>();
        while(rs.next()){
                Integer orderId = rs.getInt("ID");

                Integer customerId = rs.getInt("CUSTOMER_ID");
                if(customerId == 0 ) {
                    customerId = null;
                }
                String customerLabel = rs.getString("CUSTOMER_LABEL");
                String customerInn = rs.getString("CUSTOMER_INN");
                String customerAddress = rs.getString("CUSTOMER_ADDRESS");
                Boolean customerIsActive = new Boolean(rs.getInt("CUSTOMER_IS_ACTIVE")==1);
                Double price = rs.getDouble("PRICE");
                Double margin = rs.getDouble("MARGIN");
                String sellerLogin = rs.getString("SELLER");
                String sellerLabel = rs.getString("SELLER_LABEL");
                Date created =  new Date(rs.getTimestamp("CREATED").getTime());
                Date modified =  new Date(rs.getTimestamp("MODIFIED").getTime());
                OrderStatus status = OrderStatus.determineStatus(rs.getInt("STATUS_ID"));

                CustomerData customer = new CustomerDataImpl(customerId, customerLabel, customerInn, customerAddress,customerIsActive);
                orders.add(new OrderDataImpl(orderId, null, customer, sellerLogin, sellerLabel, status, created, modified, price, margin));
            }
        return orders;
    }


    public static final List<OrderLineData> parseOrderLines(ResultSet rs) throws SQLException{
        List<OrderLineData> orderLines = new ArrayList<OrderLineData>();
        while(rs.next()){
                Integer id = rs.getInt("ID");
                Integer itemId = rs.getInt("ITEM_ID");
                String itemName = rs.getString("ITEM_NAME");
                String itemNameRu = rs.getString("ITEM_NAME_RU");
                String supplierLabel = rs.getString("SUPPLIER_LABEL");
                String plantation = rs.getString("ITEM_PLANTATION");
                Integer length = rs.getInt("LENGTH");
                Date deliveryDate = new Date(rs.getDate("INCOMING_DATE").getTime());
                Integer itemQtty = rs.getInt("ITEM_QUANTITY");
                Double itemPriceRUR = rs.getDouble("ITEM_PRICE_RUR");
                Double itemPrice = rs.getDouble("ITEM_PRICE");
                Integer balance = rs.getInt("BALANCE");
                Integer qtty = rs.getInt("QUANTITY");
                Double priceRur = rs.getDouble("PRICE_RUR");
                Double priceRurSum = rs.getDouble("PRICE_RUR_SUM");
                Double margin = rs.getDouble("MARGIN");
                Double marginSum = rs.getDouble("MARGIN_SUM");
                Date created =  new Date(rs.getDate("CREATED").getTime());
                Date modified =  new Date(rs.getDate("MODIFIED").getTime());

                ItemDataImpl data = new ItemDataImpl(itemId,supplierLabel, itemName, itemNameRu, length, deliveryDate,itemQtty,itemPrice,plantation,null,itemPriceRUR,balance);
                orderLines.add(new OrderLineDataImpl(id, itemId, data, qtty, priceRur, priceRurSum, margin, marginSum, created, modified));
            }
        return orderLines;
    }

}
